home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / daemons / switchd-.1 / switchd- / switchd / switchd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-02  |  2.4 KB  |  120 lines

  1. /*
  2.  *       switchd.c   monitor keyboard and switch to a vc on inactivity.
  3.  *
  4.  *       Copyright (c) 1996 by Ervin Walter <00edwalter@bsuvc.bsu.edu>
  5.  *
  6.  *       This program is free software; you can redistribute it and/or
  7.  *       modify it under the terms of the GNU General Public License
  8.  *       as published by the Free Software Foundation; either version
  9.  *       2 of the License, or (at your option) any later version.
  10.  *
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <fcntl.h>
  18. #include <sys/vt.h>
  19. #include <linux/tty.h>
  20.  
  21. #define VERSION "0.1"
  22.  
  23. void usage(int stat)
  24. {
  25.    fprintf(stderr,
  26.       "switchd version " VERSION "\n"
  27.       "Usage: switchd inactivity_delay monitor_interval target_vt\n");
  28.    exit (stat);
  29. }
  30.  
  31. void main (int argc, char **argv)
  32. {
  33.     long oldkbdcount, currkbdcount, currtime=0;
  34.     int delaybetween, delaytime, active=0, pid;
  35.     FILE *intfp;
  36.     char line[80], kbdnumstr[10];
  37.  
  38.     int fd;
  39.     int vtno     = -1;
  40.  
  41.     if (argc != 4)
  42.         usage(1);
  43.  
  44.     vtno = (int) atol(argv[3]);
  45.  
  46.     if (vtno <= 0 || vtno > MAX_NR_CONSOLES)
  47.         usage(2);
  48.  
  49.     delaytime = atoi(argv[1]);
  50.     delaybetween = atoi(argv[2]);
  51.  
  52.     if (geteuid() && getuid())
  53.     {
  54.         fprintf(stderr, "switchd: must be root.\n");
  55.         exit(3);
  56.     }
  57.  
  58.         if ((fd = open("/dev/console",O_WRONLY)) < 0) {
  59.                 perror("switchd: Can't open /dev/console\n");
  60.                 exit(4);
  61.     }
  62.     
  63.     if ((pid = fork()) < 0)
  64.         {
  65.                 perror("Couldn't fork() daemon process");
  66.                 exit(5);
  67.     }
  68.         else
  69.         if (pid != 0)
  70.             exit(0);
  71.  
  72.         /* From here on out, it's daemon all the way... */
  73.  
  74.         setsid();
  75.         chdir("/");
  76.         umask(0);
  77.         close(STDIN_FILENO);
  78.         close(STDOUT_FILENO);
  79.         close(STDERR_FILENO);
  80.  
  81.     while(1) 
  82.     {
  83.         if ((intfp = fopen("/proc/interrupts", "r")) != NULL)
  84.         {
  85.             while (fgets(line, 80, intfp) != NULL)
  86.             {
  87.                 if (strstr(line, "keyboard") != NULL)
  88.                 {
  89.                     strncpy(kbdnumstr, &line[3], 9);
  90.                     kbdnumstr[9] = '\0';
  91.                     currkbdcount = atoi(kbdnumstr);
  92.                     if (currkbdcount == oldkbdcount) 
  93.                     {
  94.                         currtime += delaybetween;
  95.                     }
  96.                     else
  97.                     {
  98.                         currtime = 0;
  99.                         active = 0;
  100.                     }
  101.                     if ((currtime >= delaytime) && !active) 
  102.                     {
  103.                         active = 1;
  104.                         if (ioctl(fd, VT_ACTIVATE, vtno) < 0) 
  105.                         {
  106.                             close(fd);
  107.                             exit(6);
  108.                         }
  109.                         (void) ioctl(fd, VT_WAITACTIVE, vtno);
  110.                     }
  111.                     oldkbdcount = currkbdcount;
  112.                 }
  113.             }
  114.         fclose(intfp);
  115.         }
  116.     usleep(delaybetween*1000000);
  117.     }
  118. }
  119.  
  120.